package store.lunangangster.nodeflow.async;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import store.lunangangster.nodeflow.async.local.WhenExecutorBuilder;
import store.lunangangster.nodeflow.config.NodeFlowConfig;
import store.lunangangster.nodeflow.config.NodeFlowConfigHolder;

/**
 * 线程池执行器代理执行类，用于处理NodeFlow中的异步问题
 *
 * @author cwk
 * @version 1.0
 */
public class ExecutorServiceDelegate {
	// key：full className
	// value：threadPool
	private static final Map<String, ExecutorService> threadPoolMap = new ConcurrentHashMap<>();

	private static final String WHEN_EXECUTOR_BUILDER_KEY = "store.lunangangster.nodeflow.async.local.WhenExecutorBuilder";

	/**
	 * 线程池执行器的初始化方法
	 * <p>主要作用是读取规则文件中线程池的配置参数，并且初始化线程池
	 */
	public static void init() {
		NodeFlowConfig config = NodeFlowConfigHolder.getConfig();
		ExecutorService whenExecutor = WhenExecutorBuilder.createExecutor(config);
		threadPoolMap.put(WHEN_EXECUTOR_BUILDER_KEY, whenExecutor);
	}

	/**
	 * 获取WhenFlow异步线程池
	 * @return	用于WhenFlow的异步线程池
	 */
	public static ExecutorService getWhenExecutor() {
		return threadPoolMap.get(WHEN_EXECUTOR_BUILDER_KEY);
	}

}
